﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class LoaiNguoiDungBUS
    {
        public void themLoaiNguoiDung(LoaiNguoiDung lndDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.LoaiNguoiDungs.InsertOnSubmit(lndDTO);

            storeDB.SubmitChanges();
        }

        public void suaLoaiNguoiDung(LoaiNguoiDung lndDTO, string maLoaiNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.LoaiNguoiDungs.Where(q => q.MaLoaiNguoiDung == maLoaiNguoiDung).First();
            query.TenLoaiNguoiDung = lndDTO.TenLoaiNguoiDung;

            storeDB.SubmitChanges();
        }

        public void xoaLoaiNguoiDung(GridView gridView)
        {
            //Lay MaLoaiNguoiDung duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaLoaiNguoiDung"));

                if (_checkBox.Checked == true)
                {
                    string maLoaiNguoiDungCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maLoaiNguoiDungCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaLoaiNguoiDung duoc chon vao mang
            string[] m_temp = new string[] { temp };

            while (temp.Length > 0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));

                var query = storeDB.LoaiNguoiDungs.Where(q => q.MaLoaiNguoiDung == maTemp).First();

                storeDB.LoaiNguoiDungs.DeleteOnSubmit(query);

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachLoaiNguoiDung();
            gridView.DataBind();
        }

        public IList<LoaiNguoiDung> layDanhSachLoaiNguoiDung()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstLoaiNguoiDung = storeDB.LoaiNguoiDungs.ToList();

            return lstLoaiNguoiDung;
        }

        public Boolean kiemtraTonTaiMaLoaiNguoiDung(string maLoaiNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.LoaiNguoiDungs.Where(q => q.MaLoaiNguoiDung == maLoaiNguoiDung).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenLoaiNguoiDung(string tenLoaiNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.LoaiNguoiDungs.Where(q => q.TenLoaiNguoiDung == tenLoaiNguoiDung).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaLoaiNguoiDung(string maLoaiNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maLoaiNguoiDung = storeDB.LoaiNguoiDungs.Where(m => m.MaLoaiNguoiDung == maLoaiNguoiDung).Select(m => m.MaLoaiNguoiDung).First();

            return _maLoaiNguoiDung;
        }

        public string layTenLoaiNguoiDung(string maLoaiNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenLoaiNguoiDung = storeDB.LoaiNguoiDungs.Where(m => m.MaLoaiNguoiDung == maLoaiNguoiDung).Select(m => m.TenLoaiNguoiDung).First();

            return tenLoaiNguoiDung;
        }

        public void themItemVaoDropDownList(DropDownList dropDownList)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.LoaiNguoiDungs.ToList();
            foreach (var q in query)
            {
                var maLoaiNguoiDung = q.MaLoaiNguoiDung;
                var tenLoaiNguoiDung = q.TenLoaiNguoiDung;

                ListItem item = new ListItem();
                item.Text = tenLoaiNguoiDung;
                item.Value = maLoaiNguoiDung;

                dropDownList.Items.Add(item);
            }
        }

        public void sapxepLoaiNguoiDung(DropDownList dropDownList, string maLoaiNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maLoaiNguoiDung = storeDB.LoaiNguoiDungs.Where(m => m.MaLoaiNguoiDung == maLoaiNguoiDung).Select(m => m.MaLoaiNguoiDung).First();

            dropDownList.SelectedValue = _maLoaiNguoiDung.ToString();
        }

        public string layMaLoaiNguoiDungTheoMaNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var maLoaiNguoiDung = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.MaLoaiNguoiDung).First();

            return maLoaiNguoiDung;
        }
    }
}
